<template>
<!-- 文件发布规则 -->
  <!-- 搜索工作栏 -->
   <div v-show="setView==='list'">
      <Search :schema="allSchemas.searchSchema" @search="setSearchParams" :show-reset="false"  @reset="setSearchParams">
        <!-- 新增等操作按钮 -->
        <template #actionMore>
          
        </template>
      </Search>
      <div style="margin: 10px 0;">
        <el-button
            type="primary"
            @click="openForm('create','')"
            v-hasPermi="['pictureRule:updateStatus']"
          >
            新增
          </el-button>
          <el-button
            type="primary"
            @click="handleDelete"
            v-hasPermi="['pictureRule:delete']"
          >
            删除
          </el-button>
      </div>

    <!-- 列表 -->
      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="loading"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        ref="TableRef"
         :selection="true"
        @register="register"
        :stripe="true"
        header-cell-class-name="custom-header"
      >
      <template #ruleName="{ row }">
        <el-button
            link
            type="primary"
          @click="openForm('view',row.id)"
          >
          {{ row.ruleName }}
          </el-button>
        </template>
        <template #reportType="{ row }">
          <span>{{ row.reportTypeName }}</span>
        </template>
        <template #specialType="{ row }">
          <span>{{ row.specialTypeName }}</span>
        </template>
        <template #resultType="{ row }">
          <span>{{ row.resultTypeName }}</span>
        </template>
        <template #outFile="{ row }">
          <span>{{ row.outFileName }}</span>
        </template>
      <template #isValid="{ row }">
          <el-switch
            v-model="row.isValid"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(row)"
          />
        </template>
        <template #action="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            :disabled="row.isValid=='0'"
            v-hasPermi="['pictureRule:updateStatus']"
          >
            编辑
          </el-button>
          <!-- <el-button
            link
            type="primary"
            @click="openDetail(row.id)"
          >
            详情
          </el-button> -->
          
        </template>
      </Table>
  </div>
  <ContentWrapForm v-if="setView==='add'||setView==='View'">
    <!-- 表单弹窗：添加/修改 -->
    <FilesReleaseForm ref="formRef"   @success="getList" @setClose="setClose" />
  </ContentWrapForm>

    <!-- 详情弹窗 -->
  <!-- <ContentWrapForm v-if="setView==='view'">
    <FilesReleaseForm ref="formRef"   @success="getList" @setClose="setClose" />
  </ContentWrapForm> -->

</template>
<script lang="ts" setup>
import { allSchemas } from './filesRelease.data';
import * as imgProduceApi from '@/api/productmange/imgProduce';

import FilesReleaseForm from './FilesReleaseForm.vue';
import FilesReleaseDetail from './FilesReleaseDetail.vue';
import { disable } from 'ol/rotationconstraint';

defineOptions({ name: 'FilesRelease' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject, tableMethods,register } = useTable(
  {
    getListApi: imgProduceApi.getpictureRulePage, // 分页接口
    delListApi: imgProduceApi.deletepictureRule, // 删除接口
    defaultParams:{ruleName:'',templateName:''} //传递默认数据
  }
)
// 获得表格的各种操作
const { getList,getSelections, setSearchParams } = tableMethods
const loading = ref(false)
const emit = defineEmits(['setViewFun']) 
const setView = ref('list');
const setViewFun=(type:string)=>{
  setView.value=type;
}
const setClose=()=>{
  setView.value='list';
}

const tableData=[
  {
    name:'机器人1',
    webHookUrl:'https://qyapi.weixin.qq.com/cgibin/webhook/send?key=c73daa86-3321-4731-a233-1abfd16f2839',
    isGroup:true,
    status:true
  }
]
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number|string) => {
  formRef.value?.open(type, id)
  emit('setViewFun',{type, id,str:'ruleMangeadd'});

}
/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number|string) => {
 nextTick(()=>{
  detailRef.value.open('update',id)
   })
  setViewFun('view');
}
const TableRef = ref() 
const handleSelectionChange = async() => {
    const ids=[]
    const dlis= await getSelections()
    dlis.forEach(item=>{
      console.log('item',item);
      ids.push(item.id)
    })
   
    return ids
}
/** 删除按钮操作 */
const handleDelete = async () => {
  //获取选中数据
 const ids = await handleSelectionChange();
    if(ids.length==0){
        message.info('请选择数据')
        return false
    }
    // 批量删除 获取id数组
    try {
      //确认删除
      await message.delConfirm()
      await imgProduceApi.deletepictureRule(ids)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }
  defineExpose({ setClose,getList }) 


/** 修改状态 */
const handleStatusChange = async (row: any) => {
  console.log(row,'000000',row.isValid);
  await imgProduceApi.updatepictureRuleValid({id:row.id,isValid:row.isValid})
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scoped>
/* 使用::v-deep调整每一行的样式 */
::deep(.el-table) {
  .el-table__row {
    font-size: 16px;
    height: 40px;
    /* 设置每一行的高度 */
    line-height: 40px;
    /* 设置行内文本的垂直对齐 */
    color: #333333;
  }
}
.titleFont {
  display: inline-block;
  font-size: 16px;
  color: #333;
  font-weight: bold;
  margin-bottom: 15px;
}

:deep(.custom-header) {
  background-color: #F2F3F5 !important;
  /* 设置表头背景色 */
  color: #333 !important;
  /* 设置表头文字颜色 */
  font-weight: bold !important;
  /* 设置表头文字加粗 */
  height: 42px;
}
</style>